package com.face.operate.jsu;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;











import com.face.bean.jsu.UsernameAndName;
import com.face.common.jsu.DeletePhoto;
import com.face.common.jsu.LostFaceSetOutid;
import com.face.sql.jsu.LostDao;
import com.face.sql.jsu.JDBCUtil;



public class LostToken_Operate {
	
	public LostDao dao  = new LostDao();
	 
	/*
	 * 保存
	 */
	public void Save(String username,String name,String id,String path){
			dao.insertData(username,name,id, path);
		
	}
	/*
	 * 通过指定的faceid查询username
	 */
	public String selectByfaceId(String id){
		return dao.selectByfaceid(id);
	}
	/*
	 * 通过指定的faceid查询username以及facepath
	 */
	public IdentityHashMap<String, String> selectUsernameAndFacepath(List<String> faceList){
		return dao.selectUsernameAndFacepath(faceList);
	}
	/*
	 * 通过faceid查询username+name(批量
	 */
	public IdentityHashMap<String, String> selectUsernameByFaceid(List<String> faceList) {
		return dao.selectUsernameByFaceid(faceList);
	}
	/*
	 * 通过指定的facepath查询faceid
	 */
	public String selectByfacePath(String path){
		return dao.selectByfacepath(path);
	}
	/*
	 * 通过指定的faceid删除一行纪录
	 */
	public void deleteById(String id){
		dao.deleteByid(id);
	}
	/*
	 * 通过指定的路径删除一行纪录
	 */
	public void deleteByPath(String path){
		dao.deleteBypath(path);
	}
	/*
	 * 用于更新faceid通过id,path映射
	 */
	public void updateById(String id,String path){
		dao.updateByid(id, path);
	}
	/*
	 * 用于更新facepath通过id,path的映射
	 */
	public void updateByPath(String id,String path){
		dao.updateBypath(id, path);
	}
	/*
	 * 用于查询所有数据
	 */
	public Map<String, String> selectAll(){
		Map<String,String> map =new HashMap<String, String>();
		map  = dao.selectAll();
		return map;
	}
	/*
	 * 用于显示Map的数据
	 */
	public void showMap(Map<String,String> map){
		for(Map.Entry<String, String> entry: map.entrySet()){
			System.out.println("Key = "+ entry.getKey()+" Value = "+entry.getValue());
		}
	}
	
	/*
	 * 用于返回指定字符串的token
	 */
	public String get_token(String tag){
		String get_token = "";
		get_token = tag.substring(tag.indexOf("face_token")+14,tag.indexOf("face_token")+46);
		return get_token;
	}
	/*
	 * 用于返回指定路径的所有为jpg或者png的图片的路径集合
	 */
	public List<String> Faceadd(String strPath){
		List<String> filelist = new ArrayList<String>();
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		if(files != null){
			for(int i = 0 ;i < files.length; i++){
				String filename = files[i].getName();
			    if(filename.endsWith("jpg")||filename.endsWith("png")){
					System.out.println("文件路径："+files[i].getAbsolutePath()+"文件名字"+filename);
					filelist.add(files[i].getAbsolutePath());
				}
			}
		}
		return filelist;
	}
	public double returnConfidence(String src){
		return Double.parseDouble(src.substring(src.indexOf("confidence")+13, src.indexOf("confidence")+17));
	}
	/*
	 * 查询一个寻人启事下的照片(通过username及name查询
	 */
	public List<String> selectPathByUserNameAndName(String username, String name) {
		return dao.selectPathByUserNameAndName(username, name);
	}
	/*
	 * 删除寻人启事中的照片
	 * 1.删除图片文件
	 * 2.在人脸集中删除相应的facetoken
	 * 3.在数据库中删除记录
	 */
	public void deleteByUsernameAndName(String saveDir,String username, String name) {
		
		Map<String, String> map = selectByUserNameAndName(username,name);
		LostFaceSetOutid lostFaceSet = LostFaceSetOutid.getInstance();
		String outerId = lostFaceSet.getLostFaceSetID();
		Face_Operate face_Operate = new Face_Operate();
		
		for(Map.Entry<String, String> entry:map.entrySet()){
			face_Operate.removeFace(outerId, entry.getKey());
			DeletePhoto.deletePhoto(saveDir+entry.getValue());
		}
		
		dao.deleteByUsernameAndName(username,name);
	}
	/*
	 * 根据username和name查询filepath以及facetoken
	 * 并保存在map中返回
	 * facetoken为key
	 */
	private Map<String, String> selectByUserNameAndName(String username,
			String name) {
		return dao.selectByUserNameAndName(username,name);
	}
	/**
	 * @param name为更新后的name值
	 * @param usernameAndName对象保存着需要修改的数据的主键
	 */
	public void updateName(String name, UsernameAndName usernameAndName) {
		
		dao.updateName(name,usernameAndName);
	}
	
}

